//
//  WBNetworkingTool.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/11.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit
import AFNetworking

enum WBNetworkingToolMethod: String {
    case GET
    case POST
}

class WBNetworkingTool: AFHTTPSessionManager {
    
    //设置为单例
    static let sharedTool:WBNetworkingTool = {
        let tool = WBNetworkingTool()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    //提供方法，请求数据
    func request(method: WBNetworkingToolMethod, urlString: String, parameters:AnyObject? , success: (response: AnyObject?)->() , failure: (error: NSError)->() ) {
        
        if method == .GET{
            
            //调用AFN自带的方法
            GET(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
            
        }else{
            //调用AFN自带的方法
            POST(urlString, parameters: parameters, progress: nil, success: { (_, response) in
                
                success(response: response)
                
                }, failure: { (_, error) in
                    
                    failure(error: error)
            })
        }
    }
}

// MARK: - 发布微博接口
extension WBNetworkingTool{
    
    //发布文字微博
    func updateSina(status:String ,success: (response: AnyObject?)->() , failure: (error: NSError)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/update.json"
        
        let params = [
            "access_token":WBUserAccountViewModel.sharedUserAccountViewModel.accessToken!,
            "status":status
        ]
        
        request(.POST, urlString: urlString, parameters: params, success: success, failure: failure)
        
    }
    
    //发布图片文字微博
    func uploadSina(status:String , images:[UIImage] ,success: (response: AnyObject?)->() , failure: (error: NSError)->()) {
        
        let urlString = "https://upload.api.weibo.com/2/statuses/upload.json"
        
        let params = [
            "source":WBAppKey,
            "access_token":WBUserAccountViewModel.sharedUserAccountViewModel.accessToken!,
            "status":status
            
        ]
        
        POST(urlString, parameters: params, constructingBodyWithBlock: { (formData) in
            
            let data = UIImagePNGRepresentation(images.first!)
            formData.appendPartWithFileData(data!, name: "pic", fileName: "sasa", mimeType: "application/octet-stream")
            
            }, progress: nil, success: { (_, response) in
                
                success(response: response)
                
        }) { (_, error) in
            
            failure(error: error)
            
        }
    }
    
}



///请求token
extension WBNetworkingTool{
    
    func getUserToken(code: String , success: (response: AnyObject?)->() , failure: (error: NSError)->()){
        
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let paras = [
            
            "client_id":WBAppKey,
            "client_secret":WBAppSecret,
            "grant_type":"authorization_code",
            "code":code,
            "redirect_uri":WBRedirect_uri
        ]
        
        request(.POST, urlString: urlString, parameters: paras, success: success, failure: failure)
        
    }
    
}

///根据token请求用户的信息 头像 昵称
extension WBNetworkingTool{
    
    func getUserInfo(userAccount: WBUserAccountModel , success: (response: AnyObject?)->() , failure: (error: NSError)->()){
        
        //创建URL
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        //设置参数
        let paras = [
            
            "access_token":userAccount.access_token!,
            "uid":userAccount.uid!
        ]
        
        request(.GET, urlString: urlString, parameters: paras, success: success, failure: failure)
        
    }
    
}

//请求首页数据
extension WBNetworkingTool{
    
    func getHomeData(since_id:Int64 ,max_id:Int64 ,success: (response: AnyObject?)->() , failure: (error: NSError)->()) {
        
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let paras = [
            "access_token":WBUserAccountViewModel.sharedUserAccountViewModel.accessToken!,
            "since_id":"\(since_id)",
            "max_id":"\(max_id)"
        ]
        
        request(.GET, urlString: urlString, parameters: paras, success: success, failure: failure)
    }
    
}


















